package manage.modules.action.service.impl;

import java.util.List;

import manage.commons.utils.Pager;
import manage.commons.utils.TableNameUtil;
import manage.modules.BaseService;
import manage.modules.action.model.ActionRange;
import manage.modules.action.service.ActionRangeService;
import manage.modules.rbac.model.ActionOfRange;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;

public class ActionRangeServiceImpl implements ActionRangeService {
	private static final String ACTIONRANGE_TABLE_PRIMARY = "rangeId";
	private static final String ACTIONRANGE_CACHE_KEY = "allActionRange";
	private BaseService baseService;

	@Override
	public ActionRange select(Integer rangeId) {
		return baseService.select(ACTIONRANGE_TABLE_PRIMARY, rangeId,
				getClazz());
	}

	@Override
	public int insert(ActionRange actionRange) {
		int count = baseService.insert(actionRange);
		clearCacheByCount(count);
		return count;
	}

	@Override
	public int update(ActionRange actionRange) {
		int count = baseService.updateFields(ACTIONRANGE_TABLE_PRIMARY,
				actionRange.getRangeId(), actionRange);
		clearCacheByCount(count);
		return count;
	}

	@Override
	public int delete(Integer rangeId) {
		int count = baseService.delete(ACTIONRANGE_TABLE_PRIMARY, rangeId,
				getClazz());
		baseService.delete(ACTIONRANGE_TABLE_PRIMARY, rangeId,
				ActionOfRange.class);
		clearCacheByCount(count);
		return count;
	}

	@Override
	public List<ActionRange> listAll() {
		List<ActionRange> allActionRange = baseService.getListByCache(
				ACTIONRANGE_CACHE_KEY, getClazz());
		if (CollectionUtils.isEmpty(allActionRange)) {
			allActionRange = baseService.listAll(getClazz());
			if (CollectionUtils.isNotEmpty(allActionRange)) {
				baseService.setCache(ACTIONRANGE_CACHE_KEY, allActionRange);
			}
		}
		return allActionRange;
	}

	@Override
	public boolean exists(Integer rangeId) {
		return baseService.exists(ACTIONRANGE_TABLE_PRIMARY, rangeId,
				getClazz());
	}

	@Override
	public Long getCountOfAll() {
		return baseService.countOfAll(getClazz());
	}

	@Override
	public void query(ActionRange actionRange, Pager<ActionRange> pager) {
		StringBuilder querySql = new StringBuilder(1024);
		querySql.append("select * from "
				+ TableNameUtil.getTableName(getClazz()) + " where 1=1");
		if (actionRange != null) {
			if (StringUtils.isNotBlank(actionRange.getRangeName())) {
				querySql.append(" and rangeName like '%"
						+ actionRange.getRangeName() + "%'");
			}
			pager.setRowCount(baseService.getJdbcOperations().queryForLong(
					baseService.getCountSql(querySql.toString())));
			querySql.append(" limit ?,?");
			ParameterizedRowMapper<ActionRange> rowMapper = ParameterizedBeanPropertyRowMapper
					.newInstance(getClazz());
			pager.setList(baseService.getJdbcOperations().query(
					querySql.toString(), rowMapper, pager.getRowOffset(),
					pager.getPageSize()));
		}
	}

	private void clearCacheByCount(int count) {
		if (count > 0) {
			baseService.clearCache(ACTIONRANGE_CACHE_KEY);
		}
	}

	public Class<ActionRange> getClazz() {
		return ActionRange.class;
	}

	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	}
}
